package com.lcy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lcy.entry.SmbmsProvider;
import com.lcy.entry.SmbmsRole;
import com.lcy.entry.SmbmsUser;
import com.lcy.service.SmbmsProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;


@Controller
public class SmbmsProviderController {

    @Autowired
    SmbmsProviderService smbmsProviderService;


    @RequestMapping("/jsp/provider.do")
    public String provider(HttpServletRequest req, HttpServletResponse resp){
        String pageIndex =req.getParameter("pageIndex");
        //String queryname =req.getParameter("queryname");
        String queryProCode = req.getParameter("queryProCode");
        String queryProName = req.getParameter("queryProName");
        //String queryUserRole = req.getParameter("queryUserRole");
        //准备数据 从数据库当中查找数据，并放到session 当中去
        //rvlet ==>service ==> dao
        Page page=new Page();
        if (pageIndex==null||pageIndex==""){
            page.setCurrent(1);
        }else {
            page.setCurrent(Long.valueOf(pageIndex));
        }
        page.setSize(5);
        SmbmsProvider smbmsProvider=new SmbmsProvider();
        if(queryProCode!=null&&queryProCode!=""){
            smbmsProvider.setProCode(queryProCode);
        }
        if(queryProName!=null&&queryProName!=""){
            smbmsProvider.setProName(queryProName);
        }
        IPage<SmbmsProvider> smbmsProviderByPage= smbmsProviderService.getSmbmsProviderByPage(page,smbmsProvider);

        HttpSession session = req.getSession();
        session.setAttribute("providerList",smbmsProviderByPage.getRecords());
        //总页数
        session.setAttribute("totalPageCount",smbmsProviderByPage.getPages());
        //总条数
        session.setAttribute("totalCount",smbmsProviderByPage.getTotal());
        //当前页
        session.setAttribute("currentPageNo",page.getCurrent());
        return "/jsp/providerlist";
    }
    @RequestMapping("/jsp/addproviderview")
    public String addproviderview(){
        return "jsp/provideradd";
    }
    @RequestMapping("/jsp/addProvider")
    public String addProvider(SmbmsProvider smbmsProvider){
        smbmsProvider.setCreatedBy(new Long(1));
        smbmsProvider.setCreationDate(new Date());
        smbmsProviderService.addSmbmsProvider(smbmsProvider);
        return "forward:/jsp/provider.do";

    }
    @RequestMapping("/jsp/delProvider")
    @ResponseBody
    public String delProvider(HttpServletRequest req,HttpServletResponse resp){
        String proid = req.getParameter("proid");
        smbmsProviderService.delSmbmsProvider(Long.valueOf(proid));
        return "{\"delResult\":\"true\"}";
    }
    @Autowired
    HttpSession httpSession;
    @RequestMapping("/jsp/getProviderById")
    public String getProviderById(String proid) {
        SmbmsProvider smbmsProvider =      smbmsProviderService.getSmbmsProviderById(Long.valueOf(proid));
        httpSession.setAttribute("provider",smbmsProvider);
        return "jsp/providerview";
    }
    @RequestMapping("/jsp/updProviderView")
    public String updProviderView(String proid) {
        SmbmsProvider smbmsProvider =      smbmsProviderService.getSmbmsProviderById(Long.valueOf(proid));
        httpSession.setAttribute("provider",smbmsProvider);
        return "jsp/providermodify";
    }
    @RequestMapping("/jsp/updProvider")
    public String updProvider(SmbmsProvider smbmsProvider) {
        smbmsProviderService.updSmbmsProvider(smbmsProvider);
        return "forward:/jsp/provider.do";
    }


}


